/************************************************************************
                          lvIME Demo
 ************************************************************************/
#include "lvgl.h"
#include <string.h>
#include "lvIME.h"

extern void lvIME_Demo(void);

 //--------------------------测试需变量---------------------------
struct _lvIME_Touch IME;  //输入法结构
static char _cBuf[256] = {'\0',};   //输入法要编辑的缓冲,退出也存储

//--------------------------重新进入菜单回调实现-------------------------
static void event_cb(lv_event_t * e)
{
  lv_obj_t * mbox = lv_event_get_user_data(e);
  lv_msgbox_close(mbox);
  //lv_msgbox_close_async(mbox);
  lvIME_Demo();
}

//--------------------------退出输入法通报函数---------------------------
void _lvIME_EndCb(void *pv, signed char IsSave)
{
  lvIME_ResetStyle();//先退出输入法样式
  //先copy出再删除编辑器
  lv_obj_t * pTextArea = (lv_obj_t *)pv;
  if(pTextArea != NULL){
    if(IsSave) strcpy(_cBuf, lv_textarea_get_text(pTextArea));//保存
     lv_obj_delete(pTextArea);
  }
  lvIME_TouchDelete(&IME);//删除自身
  LV_LOG_USER("lvIME has exited!");

  //提示退出并可重新进入
  lv_obj_t * mbox = lv_msgbox_create(lv_screen_active());
  lv_msgbox_add_text(mbox, "lvIME has exited!\n\
                             Please press the confirm button to re-enter");
  lv_obj_t * btn;
  btn = lv_msgbox_add_footer_button(mbox, "Enter");
  lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, mbox);
}

//-----------------------------Demo入口实现-------------------------------
void lvIME_Demo(void)
{
  //Demo 编辑器
  lv_obj_t * pTextArea = lv_textarea_create(lv_screen_active());
  lv_obj_align(pTextArea, LV_ALIGN_TOP_MID, 0, 10);//中上对齐
  lv_textarea_set_max_length(pTextArea, 256);
  lv_textarea_set_text(pTextArea, _cBuf);
  lvIME_InitStyle();//输入法样式
  lvIME_TouchCreateTA(&IME, _lvIME_EndCb, pTextArea, pTextArea,
                      LVIME_TYPE_SIGN, 0xFF, NULL);//开启全部输入法
}

//---------------------------外界主动删除实现-------------------------------
void lvIME_Demo_Delete(void)
{
  lvIME_ResetStyle(); //先退出输入法样式
  lvIME_TouchDelete(&IME); //删除自身
}
